package com.garbagecan.cachestudy.mycache;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class CacheManagerFactory {

	private CacheManager cacheManager;
	
	public CacheManagerFactory(File file) throws InvalidConfigurationException {
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(file));
			String className = props.getProperty("cachemanager");
			if (className == null) {
				className = "org.garbagecan.cachestudy.mycache.impl.CacheManagerJCSAdapter";
			}
			Class clazz = Class.forName(className.trim());
			cacheManager = (CacheManager) clazz.newInstance();
		} catch (FileNotFoundException e) {
			throw new InvalidConfigurationException(e);
		} catch (IOException e) {
			throw new InvalidConfigurationException(e);
		} catch (InstantiationException e) {
			throw new InvalidConfigurationException(e);
		} catch (IllegalAccessException e) {
			throw new InvalidConfigurationException(e);
		} catch (ClassNotFoundException e) {
			throw new InvalidConfigurationException(e);
		}
	}
	
	public CacheManager getCacheManager() {
		return cacheManager;
	}
}
